package module.config

class ValueConfigController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        //redirect(action:'list')
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [valueConfigInstanceList: ValueConfig.list(params), valueConfigInstanceTotal: ValueConfig.count()]
    }

    def create = {
        def valueConfigInstance = new ValueConfig()
        valueConfigInstance.properties = params
        return [valueConfigInstance: valueConfigInstance]
    }

    def save = {
        // log.debug params

        def valueConfigInstance = new ValueConfig(params)

        def now = new Date()
        valueConfigInstance.dateCreated=now
        valueConfigInstance.lastUpdated=now

        ValueConfig.withTransaction{
            if (valueConfigInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.created.message', args: [message(code: 'valueConfig.label', default: 'ValueConfig'), valueConfigInstance.id])}"
                redirect(action: "show", id: valueConfigInstance.id)
            }
            else {
                render(view: "create", model: [valueConfigInstance: valueConfigInstance])
            }
        }       
    }

    def show = {
        def valueConfigInstance = ValueConfig.get(params.id)
        if (!valueConfigInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'valueConfig.label', default: 'ValueConfig'), params.id])}"
            redirect(action: "list")
        }
        else {
            [valueConfigInstance: valueConfigInstance]
        }
    }

    def edit = {
        def valueConfigInstance = ValueConfig.get(params.id)
        if (!valueConfigInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'valueConfig.label', default: 'ValueConfig'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [valueConfigInstance: valueConfigInstance]
        }
    }

    def update = {
        ValueConfig.withTransaction{
            def valueConfigInstance = ValueConfig.get(params.id)

            valueConfigInstance.lastUpdated = new Date()

            if (valueConfigInstance) {
                if (params.version) {
                    def version = params.version.toLong()
                    if (valueConfigInstance.version > version) {

                        valueConfigInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'valueConfig.label', default: 'ValueConfig')] as Object[], "Another user has updated this ValueConfig while you were editing")
                        render(view: "edit", model: [valueConfigInstance: valueConfigInstance])
                        return
                    }
                }
                valueConfigInstance.properties = params
                if (!valueConfigInstance.hasErrors() && valueConfigInstance.save(flush: true)) {
                    flash.message = "${message(code: 'default.updated.message', args: [message(code: 'valueConfig.label', default: 'ValueConfig'), valueConfigInstance.id])}"
                    redirect(action: "show", id: valueConfigInstance.id)
                }
                else {
                    render(view: "edit", model: [valueConfigInstance: valueConfigInstance])
                }
            }
            else {
                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'valueConfig.label', default: 'ValueConfig'), params.id])}"
                redirect(action: "list")
            }
        }
    }

    def delete = {
        ValueConfig.withTransaction{
            def valueConfigInstance = ValueConfig.get(params.id)
            if (valueConfigInstance) {
                try {
                    valueConfigInstance.delete(flush: true)
                    flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'valueConfig.label', default: 'ValueConfig'), params.id])}"
                    redirect(action: "list")
                }
                catch (org.springframework.dao.DataIntegrityViolationException e) {
                    flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'valueConfig.label', default: 'ValueConfig'), params.id])}"
                    redirect(action: "show", id: params.id)
                }
            }
            else {
                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'valueConfig.label', default: 'ValueConfig'), params.id])}"
                redirect(action: "list")
            }
        }
    }
       

    //check
    def check = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)

        //render servletContext.sysConfig
        [sysConfigInstanceList: ValueConfig.list(params), sysConfigInstanceTotal: ValueConfig.count(),config:servletContext.sysConfig]
    }
    //update sys
    def updateConfig={
        servletContext.sysConfig = new module.config.SysConfigManager().getConfig()
        flash.message = "系统配置更新完成"
        redirect(action: "check")
    }
}
